from playwright.sync_api import sync_playwright
import json
try:
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context(
            user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        )
        page = context.new_page()

        # 监听网络请求
        def handle_response(response):
            if "/api/" in response.url or "ajax" in response.url:
                try:
                    body = response.body()
                    text = body.decode('utf-8')
                    data = json.loads(text)

                    # 查找screenshots.iphone数据
                    if "screenshots" in data and "iphone" in data["screenshots"]:
                        screenshots = data["screenshots"]["iphone"][:3]  # 只取前3条
                        print(json.dumps(screenshots, indent=2, ensure_ascii=False))
                except Exception as e:
                    pass  # 忽略非JSON响应

        page.on("response", handle_response)

        import sys
        url = sys.argv[1]
        page.goto(url, timeout=60000)
        page.wait_for_load_state('networkidle')
        page.wait_for_timeout(5000)

        browser.close()
except Exception as e:
    print(f"错误: {e}")
